package com.apps.ftapp.activity;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;

import com.apps.ftapp.R;
import com.apps.ftapp.loader.GlideImageLoader;
import com.apps.ftapp.model.BookDetailModel;
import com.apps.ftapp.util.ActivitySkipUtil;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.DateUtil;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.Tools;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cz.msebera.android.httpclient.Header;
import es.dmoral.toasty.Toasty;


/**
 * 图书详情
 */
public class BookDetailActivity extends BaseActivity implements OnClickListener {


    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.dool)
    TextView dool;
    @BindView(R.id.price)
    TextView price;
    @BindView(R.id.sale_num_tit)
    TextView saleNumTit;
    @BindView(R.id.sale_num)
    TextView saleNum;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.goumai)
    Button goumai;

    private String id;

    private BookDetailModel result = new BookDetailModel();

    private List<String> imgList = new ArrayList<>();
    private List<String> titleList = new ArrayList<>();


    private final Gson gson = new Gson();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_detail);
        ButterKnife.bind(this);

        ShareSDK.initSDK(this);

        setTitle("图书详情");

        shareBtn.setVisibility(View.VISIBLE);
        shareBtn.setOnClickListener(this);

        //新页面接收数据
        Bundle bundle = this.getIntent().getExtras();
        //接收name值
        id = bundle.getString("id");

        L.i("main", id);

        goumai.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {


                //判断
                if (Tools.isIntNull(result.getData().getBook().getStudentNum()) >= Tools.isIntNull(result.getData().getBook().getMaxStudentNum())) {

                    Toasty.normal(getApplicationContext(), "已超过限购数量").show();
                    return;
                }

                //


                HashMap<String, Object> map = new HashMap<>();
                map.put("id", result.getData().getBook().getId());
                map.put("icon", result.getData().getBook().getLargePicture());
                map.put("price", result.getData().getBook().getPrice());
                map.put("title", result.getData().getBook().getTitle());
                map.put("type", "book");

                ActivitySkipUtil.skipAnotherActivity(BookDetailActivity.this, OrderAddActivity.class, map);

            }
        });


        loadData(id);


    }

    private void loadData(String id) {

        RequestParams params = new RequestParams();
        params.put("id", id);
        Async.post("book/info", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    result = gson.fromJson(new String(responseBody), BookDetailModel.class);
                    if (1 == Tools.isIntNull(result.getSuccess())) {
                        imgList.add(result.getData().getBook().getLargePicture());
                        titleList.add(result.getData().getBook().getTitle());
                        //设置图片加载器
                        // banner = (Banner) view.findViewById(R.id.banner);
                        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
                        banner.setImageLoader(new GlideImageLoader());
                        //设置自动轮播，默认为true
                        banner.isAutoPlay(false);
                        //设置轮播时间
                        banner.setDelayTime(3500);
                        //设置banner动画效果
                        banner.setBannerAnimation(Transformer.Default);
                        //设置指示器位置（当banner模式中有指示器时）
                        banner.setIndicatorGravity(BannerConfig.CENTER);
                        banner.setImages(imgList);
                        banner.setBannerTitles(titleList);
                        banner.start();


                        price.setText("" + result.getData().getBook().getPrice());
                        saleNum.setText(result.getData().getBook().getStudentNum());
                        saleNumTit.setText("已购，限购" + result.getData().getBook().getMaxStudentNum());
                        time.setText(DateUtil.timeStamp2Date(result.getData().getBook().getEndShopTime(), "yyyy-MM-dd") + "停售");

                        webView.loadData(result.getData().getBook().getAbout(), "text/html; charset=UTF-8", null);
                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.shareBtn:
                share();
                break;
        }
    }

    private void share() {

        BookDetailModel.DataBean.CourseBean.ShareBean shareBean = result.getData().getCourse().getShare();
        if (null != shareBean) {
            OnekeyShare oks = new OnekeyShare();
            oks.disableSSOWhenAuthorize();
            oks.setTitle(shareBean.getTitle());
            oks.setTitleUrl(shareBean.getUrl());
            oks.setText(shareBean.getTitle());
            oks.setImageUrl(shareBean.getImage());
            oks.setUrl(shareBean.getUrl());
            oks.setSite(shareBean.getTitle());
            oks.setSiteUrl(shareBean.getUrl());
            oks.show(BookDetailActivity.this);
        }
    }
}
